﻿using System;
using System.Collections.Generic;
using System.Text;
using Bbm.Persistence.Serializers;
using NUnit.Framework;

namespace Bbm.Persistence.Tests
{
	[TestFixture]
	public class PersistenceTests
	{
		[Test]
		public void TestBasicPersistence()
		{
			var config = new CacheConfiguration(new GenericJsonSerializer(), new MemoryHog(), null);
			var context = new CacheContext(config);

			var store = context.Access<int>();

			store["1"] = 1;

			Assert.AreEqual(1, store["1"]);

			// For some reason the following tests do not work
			//Assert.That(ReferenceEquals(store, context.Access<int>()), "Stores should be consistent.");
			//Assert.That(store != context.Access<int>("domain"), "Different domains must have different stores.");
		}

		[Test]
		public void CollectionTests()
		{
			var config = new CacheConfiguration(new GenericJsonSerializer(), new MemoryHog(), null);
			var context = new CacheContext(config);

			var lists = context.Access<List<int>>();

			lists.Add("list", new List<int>());

			Assert.IsNotNull(lists["list"]);
			Assert.AreEqual(0, lists["list"].Count);

			var dictionaries = context.Access<Dictionary<int, string>>();

			var lookup = new Dictionary<int, string>();

			lookup[1] = "one";
			lookup[2] = "two";

			dictionaries.Add("lookup", lookup);

			Assert.AreEqual(2, dictionaries["lookup"].Count);
			Assert.AreEqual("one", dictionaries["lookup"][1]);
			Assert.AreEqual("two", dictionaries["lookup"][2]);
		}

		[Test]
		public void InterfaceTests()
		{
			var config = new CacheConfiguration(new GenericJsonSerializer(), new MemoryHog(), null);
			var context = new CacheContext(config);

			var lists = context.Access<IEnumerable<int>>();

			lists.Add("list", new List<int>());

			Assert.IsNotNull(lists["list"]);

			var dictionaries = context.Access<IDictionary<int, string>>();

			var lookup = new Dictionary<int, string>();

			lookup[1] = "one";
			lookup[2] = "two";

			dictionaries.Add("lookup", lookup);

			Assert.AreEqual(2, dictionaries["lookup"].Count);
			Assert.AreEqual("one", dictionaries["lookup"][1]);
			Assert.AreEqual("two", dictionaries["lookup"][2]);
		}
	}
}
